<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Qiniu\Auth;

//使用auth类
/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
header('content-type:text/html;charset=utf8');

class ManageController extends AdminController
{
	/**
	 * 系统非常规MD5加密方法
	 * @param        string        $str        要加密的字符串
	 * @return       string
	 */
	public function think_ucenter_md5($str, $key = 'ThinkUCenter')
	{
		return '' === $str ? '' : md5(sha1($str) . $key);
	}

	/**
	 * 会员列表
	 * */
	public function userList()
	{
		$list = M("ucenter_member")->where(' FIND_IN_SET(2, user_type) ')->select();

		$this->meta_title = '用户列表';
		$this->assign("_list", $list);
		$this->display();
	}

	public function addUser()
	{
		if (IS_POST) {
			$input = $_POST;

			M('ucenter_member')->add($input);
		} else {
			$this->display();
		}
	}

	/**
	 * 机构列表
	 * */
	public function institutionList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$name = $dataArr['name'];
			if ($name) {
				$where['name'] = array('like', '%' . $name . '%');
				$map['name'] = $name;
			}

			$mobile = $dataArr['mobile'];
			if ($mobile) {
				$where['mobile'] = array('like', '%' . $mobile . '%');
				$map['mobile'] = $mobile;
			}

			$status = $dataArr['status'];
			if ($status > -1) {
				$where['status'] = $status;
				$map['status'] = $status;
			}
		}

		$count = M('institution')->where($where)->count();
		$Page = new \Think\Page($count, 20, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('institution')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '机构列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增机构
	 * */
	public function addInstitution()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];
			$username = $input['username'];
			$mobile = $input['mobile'];
			$id_card = $input['id_card'];
			$profit_rate = $input['profit_rate'];
			$card_front = $input['card_front'];
			$card_behind = $input['card_behind'];
			$business_license = $input['business_license'];
			$desc = $input['desc'];

			if (empty($name)) $this->error('机构名称缺失');
			if (empty($username)) $this->error('负责人姓名缺失');
			if (empty($mobile)) $this->error('负责人电话缺失');
			if (empty($id_card)) $this->error('身份证号码缺失');
			if (empty($profit_rate) || $profit_rate <= 0 || $profit_rate >= 100) $this->error('请输入正确的分成比例');
			if (empty($card_front)) $this->error('请上传身份证正面');
			if (empty($card_behind)) $this->error('请上传身份证反面');
			if (empty($business_license)) $this->error('请上传营业执照');
			if (empty($desc)) $this->error('机构介绍缺失');

			$mobile_count = M('institution')->where(array('mobile' => $mobile))->count();
			if ($mobile_count) $this->error('电话已存在');

			$card_count = M('institution')->where(array('IDcard' => $id_card))->count();
			if ($card_count) $this->error('身份证号码已存在');

			$time = time();

			$data = array(
				'name' => $name,
				'username' => $username,
				'mobile' => $mobile,
				'IDcard' => $id_card,
				'profit_rate' => $profit_rate,
				'card_front' => $card_front,
				'card_behind' => $card_behind,
				'business_license' => $business_license,
				'desc' => $desc,
				'status' => 1,
				'create_time' => $time
			);

			$res = M('institution')->add($data);
			if ($res) {
				$user_data = array(
					'username' => $name,
					'password' => $this->think_ucenter_md5('123456', '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT'),
					'mobile' => $mobile,
					'iid' => $res,
					'reg_time' => time(),
					'user_type' => 3
				);
				$uid = M('ucenter_member')->add($user_data);

				$qr_path = createInstituteQr($res);
				$cover['path'] = $qr_path;
				$cover['status'] = 1;
				$cover['create_time'] = $time;
				$qr = M('picture')->add($cover);

				$up_data = array(
					'qr' => $qr,
					'uid' => $uid
				);
				M('institution')->where(array('id' => $res))->save($up_data);

				$this->success('新增机构成功', U('Manage/institutionList'));
			} else {
				$this->error('编辑机构失败');
			}
		} else {
			$this->meta_title = '新增机构';
			$this->display();
		}
	}

	/**
	 * 编辑机构
	 * */
	public function institutionDetail()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$name = $input['name'];
//			$username = $input['username'];
//			$mobile = $input['mobile'];
//			$id_card = $input['id_card'];
			$profit_rate = $input['profit_rate'];
			$desc = $input['desc'];
			$status = $input['status'];

			if (empty($id)) $this->error('ID缺失');
//			if (empty($name)) $this->error('机构名称缺失');
//			if (empty($username)) $this->error('负责人姓名缺失');
//			if (empty($mobile)) $this->error('负责人电话缺失');
//			if (empty($id_card)) $this->error('身份证号码缺失');
			if ((empty($profit_rate) || $profit_rate <= 0 || $profit_rate >= 100) && $status == 1) $this->error('请输入正确的分成比例');
//			if (empty($desc)) $this->error('机构介绍缺失');

//			$mobile_count = M('institution')->where(array('mobile' => $mobile, 'id' => array('neq', $id)))->count();
//			if ($mobile_count) $this->error('电话已存在');
//
//			$card_count = M('institution')->where(array('IDcard' => $id_card, 'id' => array('neq', $id)))->count();
//			if ($card_count) $this->error('身份证号码已存在');

			$info = M('institution')->where(array('id' => $id))->find();
			if (empty($info)) $this->error('机构不存在或已删除');

			if (!$info['qr']) {
				$qr_path = createInstituteQr($id);
				$cover['path'] = $qr_path;
				$cover['status'] = 1;
				$cover['create_time'] = time();
				$qr = M('picture')->add($cover);
			} else {
				$qr = $info['qr'];
			}

			$data = array(
				'name' => $name,
//				'username' => $username,
//				'mobile' => $mobile,
//				'IDcard' => $id_card,
				'profit_rate' => $profit_rate,
				'desc' => $desc,
				'qr' => $qr,
				'status' => $status,
				'update_time' => time()
			);

			$res = M('institution')->where(array('id' => $id))->save($data);
			if ($res) {
				$user_info = M('ucenter_member')->where(array('id' => $info['uid']))->find();
				if (strpos($user_info['user_type'], '3') !== false) {
					$add_three = '';
				} else {
					$add_three = ',3';
				}

				$up_data = array(
					'iid' => $id,
					'user_type' => $user_info['user_type'] . $add_three
				);
				M('ucenter_member')->where(array('id' => $info['uid']))->save($up_data);

				$this->success('编辑机构成功', U('Manage/institutionList'));
			} else {
				$this->error('编辑机构失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('institution')->where(array('id' => $id))->find();
			$info['banner'] = $info['banner'] ? doImgs($info['banner']) : array();
			$this->assign('info', $info);

			$this->meta_title = '编辑机构';
			$this->display();
		}
	}

	/**
	 * 删除机构
	 * */
	public function delInstitution()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('机构ID缺失');

		$count = M('institution')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('institution')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('机构删除成功');
			} else {
				$this->error('机构删除失败');
			}
		} else {
			$this->error('机构不存在或已删除');
		}
	}

	/**
	 * 面授课分类列表
	 * */
	public function faceCourseTypeList()
	{
		$tree = D('face_course_type')->getTree(0, 'id,name,sort,pid');
		$this->assign('tree', $tree);

		$this->meta_title = '面授课分类列表';
		$this->display();
	}

	/**
	 * 供应商列表
	 * */
	public function supplierList()
	{
		$count = M('supplier')->count();
		$Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('supplier')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '供应商列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增供应商
	 * */
	public function addSupplier()
	{
		if (IS_POST) {
			$input = I('post.');

			$username = $input['username'];
			$mobile = $input['mobile'];

			if (empty($username)) $this->error('请输入供应商名称');
			if (empty($mobile)) $this->error('请输入手机号');

			$count = M('supplier')->where(array('mobile' => $mobile))->count();
			if ($count > 0) $this->error('手机号已存在');

			$time = time();

			$data = array(
				'username' => $username,
				'mobile' => $mobile,
				'create_time' => $time
			);

			$res = M('supplier')->add($data);
			if ($res) {
				$user_data = array(
					'username' => $username,
					'mobile' => $mobile,
					'supplier_id' => $res,
					'password' => $this->think_ucenter_md5('123456', '{`47CxmWwv[nV/?)^L1YRz-&0%rjQAtXp53.,#qT'),
					'user_type' => 5,
					'reg_time' => $time
				);
				$uid = M('ucenter_member')->add($user_data);

				M('supplier')->where(array('id' => $res))->setField('uid', $uid);

				$this->success('新增供应商成功', U('supplierList'));
			} else {
				$this->error('新增供应商失败');
			}
		} else {
			$this->meta_title = '新增供应商';
			$this->display();
		}
	}

	/**
	 * 编辑供应商
	 * */
	public function upSupplier()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$username = $input['username'];
			$mobile = $input['mobile'];

			if (empty($username)) $this->error('请输入供应商名称');
			if (empty($mobile)) $this->error('请输入手机号');

			$count = M('supplier')->where(array('mobile' => $mobile, 'id' => array('neq', $id)))->count();
			if ($count > 0) $this->error('手机号已存在');

			$info = M('supplier')->where(array('id' => $id))->find();
			if (empty($info)) $this->error('供应商信息不存在或已删除');

			$time = time();

			$data = array(
				'username' => $username,
				'mobile' => $mobile,
				'update_time' => $time
			);

			$res = M('supplier')->where(array('id' => $id))->save($data);
			if ($res) {
				$user_data = array(
					'username' => $username,
					'mobile' => $mobile
				);
				M('ucenter_member')->where(array('id' => $info['uid']))->save($user_data);

				$this->success('编辑供应商成功', U('supplierList'));
			} else {
				$this->error('编辑供应商失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('supplier')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑供应商';
			$this->display();
		}
	}

	/**
	 * 删除供应商
	 * */
	public function delSupplier()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('供应商ID缺失');

		$info = M('supplier')->where(array('id' => $id))->find();
		if ($info) {
			$res = M('supplier')->where(array('id' => $id))->delete();
			if ($res) {
				M('ucenter_member')->where(array('id' => $info['uid']))->delete();

				$this->success('供应商删除成功');
			} else {
				$this->error('供应商删除失败');
			}
		} else {
			$this->error('供应商不存在或已删除');
		}
	}

	/**
	 * 书籍列表
	 * */
	public function goodsList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$name = $dataArr['name'];
			$supplier_id = $dataArr['supplier_id'];
			$is_recommend = $dataArr['is_recommend'];

			if ($name) $map['name'] = array('LIKE', '%' . $name . '%');
			if ($supplier_id) $map['supplier_id'] = $supplier_id;
			if ($is_recommend) $map['is_recommend'] = $is_recommend - 1;
		}
		$map['is_del'] = 0;

		$count = M('goods')->where($map)->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('goods')->where($map)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$supplier_list = M('supplier')->select();
		$this->assign('supplier_list', $supplier_list);

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '书籍列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 编辑书籍
	 * */
	public function upGoods()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$name = $input['name'];
			$press = $input['press'];
			$author = $input['author'];
			$publication_time = $input['publication_time'];
			$one_type_id = $input['one_type_id'];
			$two_type_id = $input['two_type_id'];
			$is_recommend = $input['is_recommend'];
			$detail = $input['detail'];
			$spec_list = json_decode($input['spec_list'], true);

			if (empty($id)) $this->error('书籍ID缺失');
			if (empty($name)) $this->error('请输入书籍名称');
			if (empty($press)) $this->error('请输入出版社');
			if (empty($author)) $this->error('请输入作者');
			if (empty($publication_time)) $this->error('请输入出版时间');
			if (empty($one_type_id)) $this->error('请选择一级分类');
			if (empty($two_type_id)) $this->error('请选择二级分类');
			if (empty($detail)) $this->error('请选择书籍详情');

			$db = M();
			$db->startTrans();

			$spec_res = true;
			if ($spec_list) {
				foreach ($spec_list as $v) {
					$spec_res = M('goods_spec')->where(array('id' => $v['id']))->setField('price', $v['price']);
					if (!$spec_res) break;
				}
			}

			$min_price = M('goods_spec')->where(array('goods_id' => $id))->min('price');
			$max_price = M('goods_spec')->where(array('goods_id' => $id))->max('price');

			$data = array(
				'name' => $name,
				'press' => $press,
				'author' => $author,
				'publication_time' => $publication_time,
				'one_type_id' => $one_type_id,
				'two_type_id' => $two_type_id,
				'is_recommend' => $is_recommend,
				'detail' => $detail,
				'update_time' => time()
			);
			if ($min_price) $data['min_price'] = $min_price;
			if ($max_price) $data['max_price'] = $max_price;

			$res = M('goods')->where(array('id' => $id))->save($data);
			if ($res && $spec_res) {
				$db->commit();
				$this->success('编辑书籍成功', U('goodsList'));
			} else {
				$db->rollback();
				$this->error('编辑书籍失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('goods')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$goods_type_list = M('goods_type')->field('id,name')->where(array('pid' => 0))->order('sort asc')->select();
			if ($goods_type_list) {
				foreach ($goods_type_list as &$v) {
					$son_list = M('goods_type')->field('id,name')->where(array('pid' => $v['id']))->order('sort asc')->select();
					$v['son_list'] = $son_list;
				}
			}
			$this->assign('goods_type_list', $goods_type_list);
			$this->assign('goods_type_list_json', json_encode($goods_type_list));

			//规格信息
			$spec_list = M('goods_spec')->where(array('goods_id' => $id))->select();
			if ($spec_list) {
				foreach ($spec_list as &$v) {
					$v['img'] = getPicture($v['img']);
				}
			}
			$this->assign('spec_list', $spec_list);

			$this->meta_title = '编辑书籍';
			$this->display();
		}
	}

	/**
	 * 推荐/取消推荐 书籍
	 * */
	public function goodsRecommend()
	{
		$id = $_GET['id'];
		$is_recommend = $_GET['is_recommend'];

		if (empty($id)) $this->error('书籍ID缺失');

		$count = M('goods')->where(array('id' => $id, 'is_del' => 0))->count();
		if ($count) {
			$data = array(
				'is_recommend' => $is_recommend,
				'update_time' => time()
			);

			$res = M('goods')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('操作成功');
			} else {
				$this->error('操作失败');
			}
		} else {
			$this->error('书籍不存在或已删除');
		}
	}

	/**
	 * 直播课订单
	 * */
	public function liveOrderList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$mobile = $dataArr['mobile'];
			$order_sn = $dataArr['order_sn'];

			$start_time = $dataArr['start_time'];
			$end_time = $dataArr['end_time'];

			$iid = $dataArr['iid'];
			if ($iid) {
				$where['o.iid'] = $iid;
				$map['iid'] = $iid;
			}

			if ($mobile) {
				$where['u.mobile'] = array('like', '%' . $mobile . '%');
				$map['mobile'] = $mobile;
			}
			if ($order_sn) {
				$where['o.order_sn'] = array('like', '%' . $order_sn . '%');
				$map['order_sn'] = $order_sn;
			}
			if ($start_time and empty($end_time)) {
				$where['o.pay_time'] = array('egt', strtotime($start_time));
				$map['start_time'] = $start_time;
			}
			if (empty($start_time) and $end_time) {
				$where['o.pay_time'] = array('elt', strtotime($end_time));
				$map['end_time'] = $end_time;
			}
			if ($start_time and $end_time) {
				$where['o.pay_time'] = array('between', array(strtotime($start_time), strtotime($end_time)));
				$map['start_time'] = $start_time;
				$map['end_time'] = $end_time;
			}
		}
		$where['is_pay'] = 1;
		$where['type'] = 1;
		$where['is_del'] = 0;

		$count = M('order o')->join('teach_ucenter_member u ON o.uid = u.id')->where($where)->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('order o')->join('teach_ucenter_member u ON o.uid = u.id')
			->field('o.id,o.iid,o.order_sn,u.username,u.mobile,o.pay_price,o.pay_time')
			->where($where)->order('id desc')
			->limit($Page->firstRow . ',' . $Page->listRows)
			->select();
		if ($list) {
			foreach ($list as &$v) {
				$to_id = getDbFieldValue('vice_order', array('order_id' => $v['id']), 'to_id');
				if ($to_id) $name = getDbFieldValue('live_course', array('id' => $to_id), 'name');
				$v['name'] = $name ? $name : '';

				$institution_name = getDbFieldValue('institution', array('id' => $v['iid']), 'name');
				$v['institution_name'] = $institution_name;
			}
		}

		$institution_list = M('institution')->where(array('api_status' => 1))->select();
		$this->assign('institution_list', $institution_list);

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '直播课订单';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 点播课订单
	 * */
	public function videoOrderList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$mobile = $dataArr['mobile'];
			$order_sn = $dataArr['order_sn'];

			$start_time = $dataArr['start_time'];
			$end_time = $dataArr['end_time'];

			$iid = $dataArr['iid'];
			if ($iid) {
				$where['o.iid'] = $iid;
				$map['iid'] = $iid;
			}

			if ($mobile) {
				$where['u.mobile'] = array('like', '%' . $mobile . '%');
				$map['mobile'] = $mobile;
			}
			if ($order_sn) {
				$where['o.order_sn'] = array('like', '%' . $order_sn . '%');
				$map['order_sn'] = $order_sn;
			}
			if ($start_time and empty($end_time)) {
				$where['o.pay_time'] = array('egt', strtotime($start_time));
				$map['start_time'] = $start_time;
			}
			if (empty($start_time) and $end_time) {
				$where['o.pay_time'] = array('elt', strtotime($end_time));
				$map['end_time'] = $end_time;
			}
			if ($start_time and $end_time) {
				$where['o.pay_time'] = array('between', array(strtotime($start_time), strtotime($end_time)));
				$map['start_time'] = $start_time;
				$map['end_time'] = $end_time;
			}
		}
		$where['is_pay'] = 1;
		$where['type'] = 3;
		$where['is_del'] = 0;

		$count = M('order o')->join('teach_ucenter_member u ON o.uid = u.id')->where($where)->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('order o')->join('teach_ucenter_member u ON o.uid = u.id')
			->field('o.id,o.iid,o.order_sn,u.username,u.mobile,o.pay_price,o.pay_time')
			->where($where)->order('id desc')
			->limit($Page->firstRow . ',' . $Page->listRows)
			->select();
		if ($list) {
			foreach ($list as &$v) {
				$to_id = getDbFieldValue('vice_order', array('order_id' => $v['id']), 'to_id');
				if ($to_id) $name = getDbFieldValue('video_course', array('id' => $to_id), 'name');
				$v['name'] = $name ? $name : '';

				$institution_name = getDbFieldValue('institution', array('id' => $v['iid']), 'name');
				$v['institution_name'] = $institution_name;
			}
		}

		$institution_list = M('institution')->where(array('api_status' => 1))->select();
		$this->assign('institution_list', $institution_list);

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '点播课订单';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 商品订单
	 * */
	public function goodsOrderList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$mobile = $dataArr['mobile'];
			$order_sn = $dataArr['order_sn'];

			$start_time = $dataArr['start_time'];
			$end_time = $dataArr['end_time'];

			$iid = $dataArr['iid'];
			if ($iid) {
				$where['iid'] = $iid;
				$map['iid'] = $iid;
			}

			if ($mobile) {
				$where['get_mobile'] = array('like', '%' . $mobile . '%');
				$map['mobile'] = $mobile;
			}
			if ($order_sn) {
				$where['order_sn'] = array('like', '%' . $order_sn . '%');
				$map['order_sn'] = $order_sn;
			}
			if ($start_time and empty($end_time)) {
				$where['pay_time'] = array('egt', strtotime($start_time));
				$map['start_time'] = $start_time;
			}
			if (empty($start_time) and $end_time) {
				$where['pay_time'] = array('elt', strtotime($end_time));
				$map['end_time'] = $end_time;
			}
			if ($start_time and $end_time) {
				$where['pay_time'] = array('between', array(strtotime($start_time), strtotime($end_time)));
				$map['start_time'] = $start_time;
				$map['end_time'] = $end_time;
			}
		}
		$where['is_pay'] = 1;
		$where['type'] = 4;
		$where['is_del'] = 0;

		$count = M('order')->where($where)->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('order')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		if ($list) {
			foreach ($list as &$v) {
				$to_id = getDbFieldValue('vice_order', array('order_id' => $v['id']), 'to_id');
				if ($to_id) $name = getDbFieldValue('goods', array('id' => $to_id), 'name');
				$v['name'] = $name ? $name : '';

				$institution_name = getDbFieldValue('institution', array('id' => $v['iid']), 'name');
				$v['institution_name'] = $institution_name;
			}
		}

		$institution_list = M('institution')->where(array('api_status' => 1))->select();
		$this->assign('institution_list', $institution_list);

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '商品订单';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增面授课分类
	 * */
	public function addFaceType()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];
			$sort = $input['sort'] ? $input['sort'] : 0;
			$pid = $input['pid'] ? $input['pid'] : 0;

			if (empty($name)) $this->error('请输入分类名');

			$count = M('face_course_type')->where(array('name' => $name, 'pid' => $pid))->count();
			if ($count > 0) $this->error('分类名重复，请重新输入');

			$data = array(
				'name' => $name,
				'sort' => $sort,
				'pid' => $pid,
				'create_time' => time()
			);
			$res = M('face_course_type')->add($data);
			if ($res) {
				$this->success('新增分类成功', U('Manage/faceCourseTypeList'));
			} else {
				$this->error('新增分类失败');
			}
		} else {
			$this->meta_title = '新增课程分类';
			$this->display();
		}
	}

	/**
	 * 编辑面授课分类
	 * */
	public function upFaceType()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$name = $input['name'];
			$sort = $input['sort'] ? $input['sort'] : 0;
			$pid = $input['pid'] ? $input['pid'] : 0;

			if (empty($id)) $this->error('分类ID缺失');
			if (empty($name)) $this->error('请输入分类名');

			$count = M('face_course_type')->where(array('name' => $name, 'pid' => $pid, 'id' => array('neq', $id)))->count();
			if ($count > 0) $this->error('分类名重复，请重新输入');

			$data = array(
				'name' => $name,
				'sort' => $sort,
				'pid' => $pid,
				'create_time' => time()
			);
			$res = M('face_course_type')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑分类成功', U('Manage/faceCourseTypeList'));
			} else {
				$this->error('编辑分类失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('face_course_type')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑课程分类';
			$this->display();
		}
	}

	/**
	 * 删除面授课分类
	 * */
	public function delFaceType()
	{
		$id = $_GET['id'];
		$pid = $_GET['pid'];

		if (empty($id)) $this->error('分类ID缺失');

		$count = M('face_course_type')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('face_course_type')->where(array('id' => $id))->delete();
			if ($res) {
				if ($pid == 0) {
					M('face_course_type')->where(array('pid' => $id))->delete();
				}

				$this->success('分类删除成功');
			} else {
				$this->error('分类删除失败');
			}
		} else {
			$this->error('分类不存在或已删除');
		}
	}

	/**
	 * 老师所属版块列表
	 * */
	public function sectionTeacherList()
	{
		$list = M('section_teacher')->order('id desc')->select();

		$this->meta_title = '老师所属版块列表';
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增老师所属版块
	 * */
	public function addSectionTeacher()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];

			if (empty($name)) $this->error('所属版块缺失');

			$count = M('section_teacher')->where(array('name' => $name))->count();
			if ($count > 0) $this->error('该所属版块已存在，请重新输入');

			$data = array(
				'name' => $name,
				'create_time' => time()
			);

			$res = M('section_teacher')->add($data);
			if ($res) {
				$this->success('新增老师所属版块成功', U('sectionTeacherList'));
			} else {
				$this->error('新增老师所属版块失败');
			}
		} else {
			$this->meta_title = '新增老师所属版块';
			$this->display();
		}
	}

	/**
	 * 编辑老师所属版块
	 * */
	public function upSectionTeacher()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$name = $input['name'];

			if (empty($id)) $this->error('ID缺失');
			if (empty($name)) $this->error('所属版块缺失');

			$count = M('section_teacher')->where(array('name' => $name, 'id' => array('neq', $id)))->count();
			if ($count > 0) $this->error('该所属版块已存在，请重新输入');

			$data = array(
				'name' => $name,
				'update_time' => time()
			);

			$res = M('section_teacher')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑老师所属版块成功', U('Manage/sectionTeacherList'));
			} else {
				$this->error('编辑老师所属版块成功');
			}
		} else {
			$id = $_GET['id'];

			$info = M('section_teacher')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑老师所属版块';
			$this->display();
		}
	}

	/**
	 * 删除老师所属版块
	 * */
	public function delSectionTeacher()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('资讯分类ID缺失');

		$count = M('section_teacher')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('section_teacher')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('老师所属版块删除成功');
			} else {
				$this->error('老师所属版块删除失败');
			}
		} else {
			$this->error('老师所属版块不存在或已删除');
		}
	}

	/**
	 * 课程分类列表
	 * */
	public function videoTypeList()
	{
		$tree = D('video_course_type')->getTree(0, 'id,name,sort,pid');
		$this->assign('tree', $tree);

		$this->meta_title = '课程分类列表';
		$this->display();
	}

	/**
	 * 新增课程分类
	 * */
	public function addVideoType()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];
			$sort = $input['sort'] ? $input['sort'] : 0;
			$pid = $input['pid'] ? $input['pid'] : 0;

			if (empty($name)) $this->error('请输入分类名');

			$count = M('video_course_type')->where(array('name' => $name, 'pid' => $pid))->count();
			if ($count > 0) $this->error('分类名重复，请重新输入');

			$data = array(
				'name' => $name,
				'sort' => $sort,
				'pid' => $pid,
				'create_time' => time()
			);
			$res = M('video_course_type')->add($data);
			if ($res) {
				$this->success('新增分类成功', U('Manage/videoTypeList'));
			} else {
				$this->error('新增分类失败');
			}
		} else {
			$this->meta_title = '新增课程分类';
			$this->display();
		}
	}

	/**
	 * 编辑课程分类
	 * */
	public function upVideoType()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$name = $input['name'];
			$sort = $input['sort'] ? $input['sort'] : 0;
			$pid = $input['pid'] ? $input['pid'] : 0;

			if (empty($id)) $this->error('分类ID缺失');
			if (empty($name)) $this->error('请输入分类名');

			$count = M('video_course_type')->where(array('name' => $name, 'pid' => $pid, 'id' => array('neq', $id)))->count();
			if ($count > 0) $this->error('分类名重复，请重新输入');

			$data = array(
				'name' => $name,
				'sort' => $sort,
				'pid' => $pid,
				'create_time' => time()
			);
			$res = M('video_course_type')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑分类成功', U('Manage/videoTypeList'));
			} else {
				$this->error('编辑分类失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('video_course_type')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑课程分类';
			$this->display();
		}
	}

	/**
	 * 删除课程分类
	 * */
	public function delVideoType()
	{
		$id = $_GET['id'];
		$pid = $_GET['pid'];

		if (empty($id)) $this->error('分类ID缺失');

		$count = M('video_course_type')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('video_course_type')->where(array('id' => $id))->delete();
			if ($res) {
				if ($pid == 0) {
					M('video_course_type')->where(array('pid' => $id))->delete();
				}

				$this->success('分类删除成功');
			} else {
				$this->error('分类删除失败');
			}
		} else {
			$this->error('分类不存在或已删除');
		}
	}

	/**
	 * 课程列表
	 * */
	public function videoList()
	{
		$where['iid'] = 0;
		$where['is_del'] = 0;

		$count = M('video_course')->where($where)->count();
		$Page = new \Think\Page($count, 10, $where);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('video_course')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '课程列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	public function getTwoTypeList()
	{
		$input = I('post.');

		$one_type_id = $input['one_type_id'];

		if ($one_type_id) {
			$list = M('video_course_type')->where(array('pid' => $one_type_id))->select();
			api_return(1, '获取数据成功', $list);
		} else {
			api_return(2, '无数据');
		}
	}

	public function getBookTwoTypeList()
	{
		$input = I('post.');

		$one_type = $input['one_type'];

		if ($one_type) {
			$where['one_type_id'] = $one_type;
			$two_type_list = M('goods_type')->where(array('pid' => $one_type))->select();
		}

		$where['is_del'] = 0;
		$goods_list = M('goods')->where($where)->select();

		$data = array(
			'two_type_list' => $two_type_list ? $two_type_list : array(),
			'goods_list' => $goods_list ? $goods_list : array()
		);
		api_return(1, '获取数据成功', $data);
	}

	public function getBook()
	{
		$input = I('post.');

		$one_type = $input['one_type'];
		$two_type = $input['two_type'];

		$where['is_del'] = 0;
		if ($one_type) $where['one_type_id'] = $one_type;
		if ($two_type) $where['two_type_id'] = $two_type;
		$goods_list = M('goods')->where($where)->select();
		if ($goods_list) {
			api_return(1, '获取数据成功', $goods_list);
		} else {
			api_return(2, '无数据');
		}
	}

	public function getSpecInfo()
	{
		$input = I('post.');

		$goods_id = $input['goods_id'];
		$spec = $input['spec'];

		if (empty($goods_id)) api_return(2, '商品ID缺失');
		if (empty($spec)) api_return(2, '商品规格缺失');

		$info = M('goods_spec')->field('id,goods_id,spec,price,count')->where(array('goods_id' => $goods_id, 'spec' => $spec))->find();
		if ($info) {
			if ($info['count'] == 0) {
				api_return(2, '该规格商品库存不足，请重新选择');
			} else {
				$goods_name = M('goods')->where(array('id' => $info['goods_id']))->getField('name');
				$info['goods_name'] = $goods_name;
				unset($info['goods_id']);
				unset($info['count']);

				api_return(1, '获取数据成功', $info);
			}
		} else {
			api_return(2, '未查询到商品规格信息');
		}
	}

	/**
	 * 新增课程
	 * */
	public function addVideoCourse()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];
			$one_type_id = $input['one_type_id'];
			$two_type_id = $input['two_type_id'];
			$img = $input['img'];
			$imgs = $input['imgs'];
			$price = $input['price'] ? $input['price'] : 0;
			$is_upper = $input['is_upper'];
			$desc = $input['desc'];
			$spec_ids = $input['spec_ids'];

			if (empty($name)) $this->error('请填写课程名称');
			if (empty($one_type_id) || empty($two_type_id)) $this->error('请选择完整分类');
			if (empty($img)) $this->error('请上传封面图');

			$data = array(
				'name' => $name,
				'one_type_id' => $one_type_id,
				'two_type_id' => $two_type_id,
				'img' => $img,
				'imgs' => $imgs,
				'price' => $price,
				'is_upper' => $is_upper,
				'desc' => $desc,
				'create_time' => time()
			);
			$res = M('video_course')->add($data);
			if ($res) {
				if ($spec_ids) {
					$spec_list = M('goods_spec')->where(array('id' => array('IN', $spec_ids)))->order('substring_index("' . implode(',', explode(',', $spec_ids)) . '", id, 1)')->select();
					if ($spec_list) {
						$book_data = array();
						foreach ($spec_list as &$v) {
							$goods_info = M('goods')->where(array('id' => $v['goods_id']))->find();
							$book_data[] = array(
								'video_id' => $res,
								'spec_id' => $v['id'],
								'goods_id' => $v['goods_id'],
								'img' => $v['img'],
								'spec' => $v['spec'],
								'name' => $goods_info['name'],
								'price' => $v['price'],
								'create_time' => time()
							);
						}
						M('video_book')->addAll($book_data);
					}
				}

				$this->success('新增课程成功', U('videoList'));
			} else {
				$this->error('新增课程失败');
			}
		} else {
			//课程一级分类
			$one_type_list = M('video_course_type')->where(array('pid' => 0))->select();
			$this->assign('one_type_list', $one_type_list);

			//书籍一级分类
			$book_one_type_list = M('goods_type')->where(array('pid' => 0))->select();
			$this->assign('book_one_type_list', $book_one_type_list);

			//书籍列表
			$goods_list = M('goods')->where(array('is_del' => 0))->select();
			$this->assign('goods_list', $goods_list);
			$this->assign('goods_list_json', json_encode($goods_list));

			$this->meta_title = '新增课程';
			$this->display();
		}
	}

	/**
	 * 编辑课程
	 * */
	public function upVideoCourse()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$name = $input['name'];
			$one_type_id = $input['one_type_id'];
			$two_type_id = $input['two_type_id'];
			$img = $input['img'];
			$imgs = $input['imgs'];
			$price = $input['price'] ? $input['price'] : 0;
			$is_upper = $input['is_upper'];
			$desc = $input['desc'];
			$spec_ids = $input['spec_ids'];

			if (empty($id)) $this->error('课程ID缺失');
			if (empty($name)) $this->error('请填写课程名称');
			if (empty($one_type_id) || empty($two_type_id)) $this->error('请选择完整分类');
			if (empty($img)) $this->error('请上传封面图');

			$data = array(
				'name' => $name,
				'one_type_id' => $one_type_id,
				'two_type_id' => $two_type_id,
				'img' => $img,
				'imgs' => $imgs,
				'price' => $price,
				'is_upper' => $is_upper,
				'desc' => $desc,
				'update_time' => time()
			);
			$res = M('video_course')->where(array('id' => $id))->save($data);
			if ($res) {
				if ($spec_ids) {
					$spec_ids_arr = explode(',', $spec_ids);
					if ($spec_ids_arr) {
						foreach ($spec_ids_arr as &$v) {
							$video_book_count = M('video_book')->where(array('video_id' => $id, 'spec_id' => $v))->count();
							if ($video_book_count == 0) {
								$spec_info = M('goods_spec')->where(array('id' => $v))->find();
								$goods_info = M('goods')->where(array('id' => $spec_info['goods_id']))->find();
								$video_book_data = array(
									'video_id' => $id,
									'spec_id' => $spec_info['id'],
									'goods_id' => $spec_info['goods_id'],
									'img' => $spec_info['img'],
									'spec' => $spec_info['spec'],
									'name' => $goods_info['name'],
									'price' => $spec_info['price'],
									'create_time' => time()
								);
								M('video_book')->add($video_book_data);
							}
						}
						M('video_book')->where(array('video_id' => $id, 'spec_id' => array('NOT IN', $spec_ids)))->delete();
					}
				} else {
					M('video_book')->where(array('video_id' => $id))->delete();
				}

				$this->success('编辑课程成功', U('videoList'));
			} else {
				$this->error('编辑课程失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('video_course')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$imgs_arr = doImgs($info['imgs']);
			$this->assign('imgs_arr', $imgs_arr);

			//课程一级分类
			$one_type_list = M('video_course_type')->where(array('pid' => 0))->select();
			$this->assign('one_type_list', $one_type_list);

			//课程二级分类
			$two_type_list = M('video_course_type')->where(array('pid' => $info['one_type_id']))->select();
			$this->assign('two_type_list', $two_type_list);

			//书籍一级分类
			$book_one_type_list = M('goods_type')->where(array('pid' => 0))->select();
			$this->assign('book_one_type_list', $book_one_type_list);

			//书籍列表
			$goods_list = M('goods')->where(array('is_del' => 0))->select();
			$this->assign('goods_list', $goods_list);
			$this->assign('goods_list_json', json_encode($goods_list));

			//推荐书籍列表
			$spec_show_list = M('video_book')->field('spec_id id,name goods_name,spec,price')->where(array('video_id' => $id))->select();
			$this->assign('spec_show_list', json_encode($spec_show_list));
			$this->assign('spec_ids', implode(',', array_column($spec_show_list, 'id')));

			$this->meta_title = '编辑课程';
			$this->display();
		}
	}

	/**
	 * 删除课程
	 * */
	public function delVideoCourse()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('课程ID缺失');

		$count = M('video_course')->where(array('id' => $id, 'is_del' => 0))->count();
		if ($count) {
			$res = M('video_course')->where(array('id' => $id))->save(array('is_del' => 1, 'update_time' => time()));
			if ($res) {
				$this->success('课程删除成功');
			} else {
				$this->error('课程删除失败');
			}
		} else {
			$this->error('课程不存在或已删除');
		}
	}

	/**
	 * 目录列表
	 * */
	public function videoTreeList()
	{
		$vc_id = $_GET['vc_id'];
		$pid = $_GET['pid'];

		$where['vc_id'] = $vc_id;
		if ($pid > 0) {
			$where['pid'] = $pid;
		} else {
			$where['pid'] = 0;
		}

		$count = M('video_tree')->where($where)->count();
		$Page = new \Think\Page($count, 10, $where);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('video_tree')->where($where)->order('id asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = $pid ? '视频列表' : '目录列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增目录 / 视频
	 * */
	public function addVideoTree()
	{
		if (IS_POST) {
			$input = I('post.');

			$vc_id = $input['vc_id'];
			$pid = $input['pid'];
			$name = $input['name'];

			if ($pid > 0) {
				$hash = $input['hash'];
				$key = $input['key'];
				$duration = round($input['duration']);
				$is_look = $input['is_look'];
				$look_time = $input['look_time'];
				$video_price = $input['video_price'];

				if (empty($video_price)) $is_look = 2;
				if ($is_look == 2) $look_time = $duration;

				//新增视频
				if (empty($name)) $this->error('请输入视频名称');
				if (empty($hash) || empty($key) || empty($duration)) $this->error('视频未上传或上传未完成');
				if ($is_look == 1 && $look_time < 1) $this->error('试看时间不可小于1秒');
				if ($duration < $look_time) $this->error('视频时长：' . $duration . '秒，试看时间可以超过视频时长');

				$data = array(
					'vc_id' => $vc_id,
					'pid' => $pid,
					'name' => $name,
					'hash' => $hash,
					'key' => $key,
					'type' => 2,
					'duration' => $duration,
					'is_look' => $is_look,
					'look_time' => $is_look == 0 ? 0 : $look_time,
					'create_time' => time()
				);

				$res = M('video_tree')->add($data);
				if ($res) {
					$this->success('新增视频成功', U('videoTreeList', array('vc_id' => $vc_id, 'pid' => $pid)));
				} else {
					$this->error('新增视频失败');
				}
			} else {
				if (empty($name)) $this->error('请输入目录名');

				//新增目录
				$data = array(
					'vc_id' => $vc_id,
					'name' => $name,
					'type' => 1,
					'create_time' => time()
				);

				$res = M('video_tree')->add($data);
				if ($res) {
					$this->success('新增目录成功', U('videoTreeList', array('vc_id' => $vc_id)));
				} else {
					$this->error('新增目录失败');
				}
			}
		} else {
			$vc_id = $_GET['vc_id'];
			$pid = $_GET['pid'] ? $_GET['pid'] : 0;

			if (empty($vc_id)) $this->error('课程ID缺失');

			if ($pid) {
				$video_course_info = M('video_course')->where(array('id' => $vc_id))->find();
				$this->assign('video_price', $video_course_info['price'] > 0 ? $video_course_info['price'] : null);

				vendor('qiniuyun/autoload');
				//私有空间视频播放链接
				$accessKey = C('AK');  //秘钥
				$secretKey = C('SK');  //秘钥
				$auth = new Auth($accessKey, $secretKey);  //实例化

				//分配token到页面
				$bucket = C('BUCKET');//存储空间
				$token = $auth->uploadToken($bucket);
				$this->assign('token', $token);

				$this->meta_title = '新增视频';
			} else {
				$this->meta_title = '新增目录';
			}

			$this->display();
		}
	}

	/**
	 * 编辑目录 / 视频
	 * */
	public function upVideoTree()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$vc_id = $input['vc_id'];
			$pid = $input['pid'];
			$name = $input['name'];

			if ($pid > 0) {
				//编辑视频
				$hash = $input['hash'];
				$key = $input['key'];
				$duration = round($input['duration']);
				$is_look = $input['is_look'];
				$look_time = $input['look_time'];
				$video_price = $input['video_price'];

				if (empty($video_price)) $is_look = 2;
				if ($is_look == 2) $look_time = $duration;

				if (empty($name)) $this->error('请输入视频名称');
				if (empty($hash) || empty($key) || empty($duration)) $this->error('视频未上传或上传未完成');
				if ($is_look == 1 && $look_time < 1) $this->error('试看时间不可小于1秒');
				if ($duration < $look_time) $this->error('视频时长：' . $duration . '秒，试看时间可以超过视频时长');

				$data = array(
					'vc_id' => $vc_id,
					'pid' => $pid,
					'name' => $name,
					'hash' => $hash,
					'key' => $key,
					'duration' => $duration,
					'is_look' => $is_look,
					'look_time' => $is_look == 0 ? 0 : $look_time,
					'update_time' => time()
				);
				$res = M('video_tree')->where(array('id' => $id))->save($data);
				if ($res) {
					$this->success('编辑视频成功', U('videoTreeList', array('vc_id' => $vc_id, 'pid' => $pid)));
				} else {
					$this->error('编辑视频失败');
				}
			} else {
				if (empty($id)) $this->error('目录ID缺失');
				if (empty($name)) $this->error('请输入目录名');

				//编辑目录
				$data = array(
					'name' => $name,
					'update_time' => time()
				);
				$res = M('video_tree')->where(array('id' => $id))->save($data);
				if ($res) {
					$this->success('编辑目录成功', U('videoTreeList', array('vc_id' => $vc_id)));
				} else {
					$this->error('编辑目录失败');
				}
			}
		} else {
			$id = $_GET['id'];
			$vc_id = $_GET['vc_id'];
			$pid = $_GET['pid'] ? $_GET['pid'] : 0;

			if (empty($id)) $this->error('目录ID缺失');
			if (empty($vc_id)) $this->error('课程ID缺失');

			$info = M('video_tree')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			if (empty($vc_id)) $this->error('课程ID缺失');

			if ($pid) {
				$video_course_info = M('video_course')->where(array('id' => $vc_id))->find();
				$this->assign('video_price', $video_course_info['price'] > 0 ? $video_course_info['price'] : null);

				vendor('qiniuyun/autoload');
				//私有空间视频播放链接
				$accessKey = 'apdlqbtjh2HOr0UElPeE_dAaR-wJPTxJvRvpvLCi';  //秘钥
				$secretKey = 'LvKx71symBnb7vXIxMT5SpqDiAlH1D4TKr73LXjk';  //秘钥
				$auth = new Auth($accessKey, $secretKey);  //实例化

				//分配token到页面
				$bucket = 'alijuly';//存储空间
				$token = $auth->uploadToken($bucket);
				$this->assign('token', $token);

				$this->meta_title = '新增视频';
			} else {
				$this->meta_title = '新增目录';
			}
			$this->display();
		}
	}

	/**
	 * 删除目录
	 * */
	public function delVideoTree()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('ID缺失');

		$count = M('video_tree')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('video_tree')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		} else {
			$this->error('不存在或已删除');
		}
	}

	/**
	 * 视频习题列表
	 * */
	public function videoTreeExercisesList()
	{
		$id = $_GET['id'];//video_tree主键id

		$exercises_ids = M('video_tree')->where(array('id' => $id))->getField('exercises_ids');

		//已选习题列表
		$check_where['vt_id'] = $id;
		$check_count = M('video_tree_exercises')->where($check_where)->count();
		$check_page = new \Think\Page($check_count, 10, $check_where);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$check_list = M('video_tree_exercises v')->join('teach_exercises e ON v.exercises_id = e.id')
			->field('v.id,e.sid,e.one_chapter_id,e.two_chapter_id,e.question,e.option,e.analysis,e.style')
			->where($check_where)->order('v.id desc')
			->limit($check_page->firstRow . ',' . $check_page->listRows)
			->select();

		$check_show = $check_page->show();// 分页显示输出
		$this->assign("check_page", $check_show);
		$this->assign('check_list', $check_list);

		$dataArr = $_REQUEST;
		if ($dataArr) {
			$sid = $dataArr['sid'];
			$one_chapter_id = $dataArr['one_chapter_id'];
			$two_chapter_id = $dataArr['two_chapter_id'];
			$style = $dataArr['style'];

			if ($sid) {
				$where['sid'] = $sid;
				$map['sid'] = $sid;
			}

			if ($one_chapter_id) {
				$where['one_chapter_id'] = $one_chapter_id;
				$map['one_chapter_id'] = $one_chapter_id;
			}

			if ($two_chapter_id) {
				$where['two_chapter_id'] = $two_chapter_id;
				$map['two_chapter_id'] = $two_chapter_id;
			}

			if ($style) {
				$where['style'] = $style;
				$map['style'] = $style;
			}
		}
		if ($exercises_ids) $where['id'] = array('NOT IN', $exercises_ids);

		$count = M('exercises')->where($where)->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('exercises')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		$show = $Page->show();// 分页显示输出

		$subject_list = M('subject')->field('id,name')->order('sort asc')->select();
		if ($subject_list) {
			foreach ($subject_list as &$val) {
				//章节-知识点 列表
				$chapter_list = M('chapter')->field('id,name')->where(array('sid' => $val['id'], 'pid' => 0))->order('sort asc')->select();
				if ($chapter_list) {
					foreach ($chapter_list as &$v) {
						$son_list = M('chapter')->field('id,name')->where(array('pid' => $v['id']))->order('sort asc')->select();
						$v['son_list'] = $son_list;
					}
				} else {
					$chapter_list = array();
				}
				$val['chapter_list'] = $chapter_list;
			}
		}
		$this->assign('subject_list', $subject_list);
		$this->assign('subject_list_json', json_encode($subject_list));

		$this->meta_title = '视频习题列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增视频习题
	 * */
	public function addVideoTreeExercises()
	{
		$input = I('post.');

		$id = $input['id'];
		$ids = $input['ids'];

		$exercises_ids = M('video_tree')->where(array('id' => $id))->getField('exercises_ids');

		$new_exercises_ids = ($exercises_ids ? $exercises_ids . ',' : '') . $ids;
		$res = M('video_tree')->where(array('id' => $id))->setField('exercises_ids', $new_exercises_ids);
		if ($res) {
			$ids_arr = explode(',', $ids);
			if ($ids_arr) {
				$data = array();
				foreach ($ids_arr as &$v) {
					$data[] = array(
						'vt_id' => $id,
						'exercises_id' => $v,
						'create_time' => time()
					);
				}
				if ($data) M('video_tree_exercises')->addAll($data);

				api_return(1, '新增习题成功');
			} else {
				api_return(2, '请勾选需要新增的习题');
			}
		} else {
			api_return(2, '新增习题失败');
		}
	}

	/**
	 * 删除视频习题
	 * */
	public function delVideoTreeExercises()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('视频习题ID缺失');

		$info = M('video_tree_exercises')->where(array('id' => $id))->find();
		if ($info) {
			$res = M('video_tree_exercises')->where(array('id' => $id))->delete();
			if ($res) {
				$list = M('video_tree_exercises')->field('id')->where(array('vt_id' => $info['vt_id']))->select();
				$exercises_ids = implode(',', array_column($list, 'id'));
				M('video_tree')->where(array('id' => $info['vt_id']))->save(array('exercises_ids' => $exercises_ids, 'update_time' => time()));

				$this->success('删除视频习题成功', U('videoTreeExercisesList', array('id' => $info['vt_id'])));
			} else {
				$this->error('删除视频习题失败');
			}
		} else {
			$this->error('视频习题不存在或已删除');
		}
	}

	/**
	 * 直播课程列表
	 * */
	public function liveList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$name = $dataArr['name'];
			$audit_status = $dataArr['audit_status'];

			if ($name) {
				$map['name'] = array('LIKE', '%' . $name . '%');
			}
			if ($audit_status) {
				$map['audit_status'] = $audit_status - 1;
			}
		}
		$map['is_del'] = 0;

		$count = M('live_course')->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('live_course')->where($map)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '直播课程列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 直播详情
	 * */
	public function liveDetail()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$audit_status = $input['audit_status'];

			if (empty($id)) $this->error('直播ID缺失');

			$data = array(
				'status' => 1,
				'audit_status' => $audit_status,
				'update_time' => time()
			);

			$res = M('live_course')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑成功', U('liveList'));
			} else {
				$this->error('编辑失败或未编辑');
			}
		} else {
			$id = $_GET['id'];

			if (empty($id)) $this->error('直播课程ID缺失');

			$info = M('live_course')->where(array('id' => $id, 'is_del' => 0))->find();
			if ($info) {
				$this->assign('info', $info);
				$this->display();
			} else {
				$this->error('直播课程不存在或已删除');
			}
		}
	}

	/**
	 * 问答管理
	 * */
	public function videoQuestionList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$name = $dataArr['name'];
			if ($name) {
				$where['c.name'] = array('LIKE', '%'. $name .'%');
				$map['name'] = $name;
			}
		}

		$count = M('video_question q')
			->join('teach_video_tree t ON q.vt_id = t.id', 'left')
			->join('teach_video_course c ON t.vc_id = c.id', 'left')->count();

		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('video_question q')
			->join('teach_video_tree t ON q.vt_id = t.id', 'left')
			->join('teach_video_course c ON t.vc_id = c.id', 'left')
			->field('q.id,q.uid,q.question,q.answer,q.create_time,c.name,t.name video_name')
			->where($where)
			->order('q.create_time desc')
			->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '问答管理';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 答复
	 * */
	public function upVideoQuestion()
	{
		if(IS_POST){
			$input = I('post.');

			$id = $input['id'];
			$answer = $input['answer'];

			if(empty($id)) $this->error('问题ID缺失');
			if(empty($answer)) $this->error('请输入回答内容');

			$data = array(
				'answer' => $answer,
				'update_time' => time()
			);

			$res = M('video_question')->where(array('id' => $id))->save($data);
			if($res){
				$this->success('操作成功', U('videoQuestionList'));
			}else{
				$this->error('操作失败');
			}
		}else{
			$id = $_GET['id'];
			$info = M('video_question')->where(array('id' => $id))->find();
			if($info){
				$video_info = M('video_tree')->where(array('id' => $info['vt_id']))->find();
				$info['video_name'] = $video_info['name'];

				$course_info = M('video_course')->where(array('id' => $video_info['vc_id']))->find();
				$info['name'] = $course_info['name'];

				$info['imgs'] = $info['imgs'] ? doImgs($info['imgs']) : [];
			}

			$this->meta_title = '问答管理';
			$this->assign("info", $info);
			$this->display();
		}
	}

	/**
	 * 学科列表
	 * */
	public function subjectList()
	{
		$count = M('subject')->count();
		$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('subject')->order('sort asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '学科列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增学科
	 * */
	public function addSubject()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];

			if (empty($name)) $this->error('名称缺失');

			$count = M('subject')->where(array('name' => $name))->count();
			if ($count) $this->error('该名称已存在');

			$data = array(
				'name' => $name,
			);

			$res = M('subject')->add($data);
			if ($res) {
				$this->success('新增学科成功', U('subjectList'));
			} else {
				$this->error('新增学科失败');
			}
		} else {
			$this->meta_title = '新增学科';
			$this->display();
		}
	}

	/**
	 * 编辑学科
	 * */
	public function upSubject()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$name = $input['name'];

			if (empty($id)) $this->error('ID缺失');

			if (empty($name)) $this->error('名称缺失');

			$count = M('subject')->where(array('name' => $name, 'id' => array('neq', $id)))->count();
			if ($count) $this->error('该名称已存在');

			$data = array(
				'name' => $name,
			);

			$res = M('subject')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑学科成功', U('Manage/subjectList'));
			} else {
				$this->error('编辑学科失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('subject')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑学科';
			$this->display();
		}
	}

	/**
	 * 删除学科
	 * */
	public function delSubject()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('学科ID缺失');

		$count = M('subject')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('subject')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('学科删除成功');
			} else {
				$this->error('学科删除失败');
			}
		} else {
			$this->error('学科不存在或已删除');
		}
	}

	/**
	 * 显示分类树，仅支持内部调
	 * @param        array        $tree        分类树
	 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
	 */
	public function chapterTree($tree = null)
	{
		C('_SYS_GET_CATEGORY_TREE_');
		$this->assign('tree', $tree);
		$this->display('chapterTree');
	}

	/**
	 * 章节-知识点 列表
	 * */
	public function chapterList()
	{
		$sid = $_GET['id'];
		$tree = D('chapter')->getTree($sid, 0, 'id,name,sort,pid');
		$this->assign('tree', $tree);

		$this->meta_title = '章节-知识点 列表';
		$this->display();
	}

	/**
	 * 新增章节-知识点
	 * */
	public function addChapter()
	{
		if (IS_POST) {
			$input = I('post.');

			$sid = $input['sid'];
			$pid = $input['pid'] ? $input['pid'] : 0;
			$name = $input['name'];
			$sort = $input['sort'];

			if (empty($sid)) $this->error('学科ID缺失');
			if (empty($name)) $this->error('名称缺失');

			if ($pid > 0) {
				$count = M('chapter')->where(array('sid' => $sid, 'pid' => $pid, 'name' => $name))->count();
				if ($count) $this->error('该章节已存在该知识点');
			} else {
				$count = M('chapter')->where(array('sid' => $sid, 'pid' => 0, 'name' => $name))->count();
				if ($count) $this->error('已存在该章节');
			}

			$data = array(
				'sid' => $sid,
				'pid' => $pid,
				'name' => $name,
				'sort' => $sort,
				'create_time' => time()
			);

			$res = M('chapter')->add($data);
			if ($res) {
				$this->success('新增成功', U('chapterList', array('id' => $sid)));
			} else {
				$this->error('新增失败');
			}
		} else {
			$this->meta_title = $_GET['pid'] ? '新增知识点' : '新增章节';
			$this->display();
		}
	}

	/**
	 * 编辑章节-知识点
	 * */
	public function upChapter()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$sid = $input['sid'];
			$pid = $input['pid'] ? $input['pid'] : 0;
			$name = $input['name'];
			$sort = $input['sort'];

			if (empty($id)) $this->error('ID缺失');
			if (empty($sid)) $this->error('学科ID缺失');
			if (empty($name)) $this->error('名称缺失');

			if ($pid > 0) {
				$count = M('chapter')->where(array('sid' => $sid, 'pid' => $pid, 'name' => $name, 'id' => array('neq', $id)))->count();
				if ($count) $this->error('该章节已存在该知识点');
			} else {
				$count = M('chapter')->where(array('sid' => $sid, 'pid' => 0, 'name' => $name, 'id' => array('neq', $id)))->count();
				if ($count) $this->error('已存在该章节');
			}

			$data = array(
				'sid' => $sid,
				'pid' => $pid,
				'name' => $name,
				'sort' => $sort,
				'update_time' => time()
			);

			$res = M('chapter')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑成功', U('Manage/chapterList', array('id' => $sid)));
			} else {
				$this->error('编辑失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('chapter')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑章节-知识点';
			$this->display();
		}
	}

	/**
	 * 删除 章节-知识点
	 * */
	public function delChapter()
	{
		$id = $_GET['id'];
		$pid = $_GET['pid'] ? $_GET['pid'] : 0;

		if (empty($id)) $this->error('ID缺失');

		$count = M('chapter')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('chapter')->where(array('id' => $id))->delete();
			if ($res) {
				if ($pid == 0) M('chapter')->where(array('pid' => $id))->delete();
				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		} else {
			$this->error('数据不存在或已删除');
		}
	}

	/**
	 * 习题列表
	 * */
	public function exercisesList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$sid = $dataArr['sid'];
			$one_chapter_id = $dataArr['one_chapter_id'];
			$two_chapter_id = $dataArr['two_chapter_id'];
			$style = $dataArr['style'];

			if ($sid) {
				$where['sid'] = $sid;
				$map['sid'] = $sid;
			}

			if ($one_chapter_id) {
				$where['one_chapter_id'] = $one_chapter_id;
				$map['one_chapter_id'] = $one_chapter_id;
			}

			if ($two_chapter_id) {
				$where['two_chapter_id'] = $two_chapter_id;
				$map['two_chapter_id'] = $two_chapter_id;
			}

			if ($style) {
				$where['style'] = $style;
				$map['style'] = $style;
			}
		}

		$count = M('exercises')->where($where)->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('exercises')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		$show = $Page->show();// 分页显示输出

		$subject_list = M('subject')->field('id,name')->order('sort asc')->select();
		if ($subject_list) {
			foreach ($subject_list as &$val) {
				//章节-知识点 列表
				$chapter_list = M('chapter')->field('id,name')->where(array('sid' => $val['id'], 'pid' => 0))->order('sort asc')->select();
				if ($chapter_list) {
					foreach ($chapter_list as &$v) {
						$son_list = M('chapter')->field('id,name')->where(array('pid' => $v['id']))->order('sort asc')->select();
						$v['son_list'] = $son_list;
					}
				} else {
					$chapter_list = array();
				}
				$val['chapter_list'] = $chapter_list;
			}
		}
		$this->assign('subject_list', $subject_list);
		$this->assign('subject_list_json', json_encode($subject_list));

		$this->meta_title = '习题列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增习题
	 * */
	public function addExercises()
	{
		if (IS_POST) {
			$input = I('post.');

			$sid = $input['sid'];
			$one_chapter_id = $input['one_chapter_id'];
			$two_chapter_id = $input['two_chapter_id'];
			$question = $input['question'];
			$option = $input['option'];
			$error_level = $input['error_level'];
			$analysis = $input['analysis'];
			$style = $input['style'];

			if (empty($sid)) $this->error('请选择学科');
			if (empty($one_chapter_id) || empty($two_chapter_id)) $this->error('请选择完整章节-知识点');
			if (empty($question)) $this->error('请输入习题内容');
			if (empty($option)) $this->error('请添加习题答案');

			$data = array(
				'sid' => $sid,
				'one_chapter_id' => $one_chapter_id,
				'two_chapter_id' => $two_chapter_id,
				'question' => $question,
				'option' => $option,
				'error_level' => $error_level,
				'analysis' => $analysis,
				'style' => $style,
				'create_time' => time()
			);

			$res = M('exercises')->add($data);
			if ($res) {
				$this->success('新增习题成功', U('exercisesList'));
			} else {
				$this->error('新增习题失败');
			}
		} else {
			$subject_list = M('subject')->field('id,name')->order('sort asc')->select();
			if ($subject_list) {
				foreach ($subject_list as &$val) {
					//章节-知识点 列表
					$chapter_list = M('chapter')->field('id,name')->where(array('sid' => $val['id'], 'pid' => 0))->order('sort asc')->select();
					if ($chapter_list) {
						foreach ($chapter_list as &$v) {
							$son_list = M('chapter')->field('id,name')->where(array('pid' => $v['id']))->order('sort asc')->select();
							$v['son_list'] = $son_list;
						}
					} else {
						$chapter_list = array();
					}
					$val['chapter_list'] = $chapter_list;
				}
			}
			$this->assign('subject_list', $subject_list);
			$this->assign('subject_list_json', json_encode($subject_list));

			$this->meta_title = '新增习题';
			$this->display();
		}
	}

	public function upExercises()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$sid = $input['sid'];
			$one_chapter_id = $input['one_chapter_id'];
			$two_chapter_id = $input['two_chapter_id'];
			$question = $input['question'];
			$option = $input['option'];
			$error_level = $input['error_level'];
			$analysis = $input['analysis'];
			$style = $input['style'];

			if (empty($id)) $this->error('习题ID缺失');
			if (empty($sid)) $this->error('请选择学科');
			if (empty($one_chapter_id) || empty($two_chapter_id)) $this->error('请选择完整章节-知识点');
			if (empty($question)) $this->error('请输入习题内容');
			if (empty($option)) $this->error('请添加习题答案');

			$data = array(
				'sid' => $sid,
				'one_chapter_id' => $one_chapter_id,
				'two_chapter_id' => $two_chapter_id,
				'question' => $question,
				'option' => $option,
				'error_level' => $error_level,
				'analysis' => $analysis,
				'style' => $style,
				'create_time' => time()
			);
			$res = M('exercises')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑习题成功', U('exercisesList'));
			} else {
				$this->error('编辑习题失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('exercises')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$subject_list = M('subject')->field('id,name')->order('sort asc')->select();
			if ($subject_list) {
				foreach ($subject_list as &$val) {
					//章节-知识点 列表
					$chapter_list = M('chapter')->field('id,name')->where(array('sid' => $val['id'], 'pid' => 0))->order('sort asc')->select();
					if ($chapter_list) {
						foreach ($chapter_list as &$v) {
							$son_list = M('chapter')->field('id,name')->where(array('pid' => $v['id']))->order('sort asc')->select();
							$v['son_list'] = $son_list;
						}
					} else {
						$chapter_list = array();
					}
					$val['chapter_list'] = $chapter_list;
				}
			}
			$this->assign('subject_list', $subject_list);
			$this->assign('subject_list_json', json_encode($subject_list));

			$this->meta_title = '编辑习题';
			$this->display();
		}
	}

	/**
	 * 删除习题
	 * */
	public function delExercises()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('习题ID缺失');

		$count = M('exercises')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('exercises')->where(array('id' => $id))->delete();
			if ($res) {
				$this->success('习题删除成功');
			} else {
				$this->error('习题删除失败');
			}
		} else {
			$this->error('习题不存在或已删除');
		}
	}

	/**
	 * 练习册/试卷 列表
	 * */
	public function practiceList()
	{
		$dataArr = $_REQUEST;
		if ($dataArr) {
			$name = $dataArr['name'];
			$sid = $dataArr['sid'];
			$style = $dataArr['style'];

			if ($name) {
				$where['name'] = array('LIKE', '%' . $name . '%');
				$map['name'] = array('LIKE', '%' . $name . '%');
			}
			if ($sid) {
				$where['sid'] = $sid;
				$map['sid'] = $sid;
			}
			if ($style) {
				$where['style'] = $style;
				$map['style'] = $style;
			}
		}

		$count = M('practice')->where($where)->count();
		$Page = new \Think\Page($count, 10, $map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('practice')->where($where)->order('sort asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		if ($list) {
			foreach ($list as &$v) {
				$v['subject_name'] = M('subject')->where(array('id' => $v['sid']))->getField('name');
			}
		}

		$subject_list = M('subject')->order('sort asc')->select();
		$this->assign('subject_list', $subject_list);

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '练习册/试卷 列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增 练习册/试卷
	 * */
	public function addPractice()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];
			$sid = $input['sid'];
			$one_chapter_id = $input['one_chapter_id'];
			$two_chapter_id = $input['two_chapter_id'];
			$year = $input['year'];
			$single_count = $input['single_count'];
			$single_score = $input['single_score'];
			$check_count = $input['check_count'];
			$check_score = $input['check_score'];
			$score = $input['score'];
			$tags = str_replace('，', ',', $input['tags']);
			$is_hot = $input['is_hot'];
			$type = $input['type'];
			$style = $input['style'];
			$sort = $input['sort'];
			$desc = $input['desc'];

			if (empty($name)) $this->error('请输入名称');
			if (empty($sid)) $this->error('请选择所属学科');
			if (empty($one_chapter_id) || empty($two_chapter_id)) $this->error('请选择完整章节-知识点');
			if (empty($single_count) && empty($check_count)) $this->error('单选，多选题数量请至少保持一项数量大于0');
			if (empty($single_score)) $this->error('请输入单选题单题分数');
			if (empty($check_score)) $this->error('请输入多选题单题分数');
			if (empty($score)) $this->error('分数错误');

			$data = array(
				'name' => $name,
				'sid' => $sid,
				'one_chapter_id' => $one_chapter_id,
				'two_chapter_id' => $two_chapter_id,
				'year' => $year,
				'single_count' => $single_count,
				'single_score' => $single_score,
				'check_count' => $check_count,
				'check_score' => $check_score,
				'score' => $score,
				'tags' => $tags,
				'is_hot' => $is_hot,
				'type' => $type,
				'style' => $style,
				'sort' => $sort,
				'desc' => $desc,
				'create_time' => time()
			);

			$Model = M();
			$Model->startTrans();
			try {
				$res = $Model->table('teach_practice')->add($data);

				$new_single_score = 0;
				if ($single_count) {
					//单选习题随机列表
					$single_list = $Model->table('teach_exercises')->field('id,RAND() r')->where(array('one_chapter_id' => $one_chapter_id, 'two_chapter_id' => $two_chapter_id, 'style' => 1))->order('r asc')->limit($single_count)->select();
					if ($single_list) {
						$single_data = array();
						foreach ($single_list as &$v) {
							$single_data[] = array(
								'practice_id' => $res,
								'exercises_id' => $v['id'],
								'sort' => 0,
								'update_time' => 0,
								'create_time' => time()
							);
						}
						$single_res = $Model->table('teach_practice_exercises')->addAll($single_data);

						$new_single_score = count($single_list) * $single_score;
					} else {
						$single_list = array();
					}
				} else {
					$single_list = array();
				}

				$new_check_score = 0;
				if ($check_count) {
					//多选习题随机列表
					$check_list = $Model->table('teach_exercises')->field('id,RAND() r')->where(array('one_chapter_id' => $one_chapter_id, 'two_chapter_id' => $two_chapter_id, 'style' => 2))->order('r asc')->limit($check_count)->select();
					if ($check_list) {
						$check_data = array();
						foreach ($check_list as &$v) {
							$check_data[] = array(
								'practice_id' => $res,
								'exercises_id' => $v['id'],
								'sort' => 0,
								'update_time' => 0,
								'create_time' => time()
							);
						}
						$check_res = $Model->table('teach_practice_exercises')->addAll($check_data);

						$new_check_score = count($check_list) * $check_score;
					} else {
						$check_list = array();
					}
				} else {
					$check_list = array();
				}

				$up_data = array(
					'single_count' => count($single_list),
					'single_score' => $single_score,
					'check_count' => count($check_list),
					'check_score' => $check_score,
					'score' => $new_single_score + $new_check_score,
					'update_time' => time()
				);
				$up_res = $Model->table('teach_practice')->where(array('id' => $res))->save($up_data);

				if ($res && (empty($single_list) || $single_res) && (empty($check_list) || $check_res) && ($single_list || $check_list) && $up_res) {
					$Model->commit();
					$this->success('新增成功', U('practiceList'));
				} else {
					$Model->rollback();
					if (empty($single_list) && empty($check_list)) {
						$this->error('所选 章节-知识点 为检索到习题');
					} else {
						$this->error('新增失败');
					}
				}
			} catch (\Exception $e) {
				$this->error('系统繁忙，请稍后再试');
			}
		} else {
			$subject_list = M('subject')->field('id,name')->order('sort asc')->select();
			if ($subject_list) {
				foreach ($subject_list as &$val) {
					//章节-知识点 列表
					$chapter_list = M('chapter')->field('id,name')->where(array('sid' => $val['id'], 'pid' => 0))->order('sort asc')->select();
					if ($chapter_list) {
						foreach ($chapter_list as &$v) {
							$son_list = M('chapter')->field('id,name')->where(array('pid' => $v['id']))->order('sort asc')->select();
							$v['son_list'] = $son_list;
						}
					} else {
						$chapter_list = array();
					}
					$val['chapter_list'] = $chapter_list;
				}
			}
			$this->assign('subject_list', $subject_list);
			$this->assign('subject_list_json', json_encode($subject_list));

			//年份
			$year = date('Y');
			$year_list = array();
			for ($i = $year; $i > $year - 15; $i--) {
				$year_list[] = $i;
			}
			$this->assign('year_list', $year_list);

			$this->meta_title = '新增 练习册/试卷';
			$this->display();
		}
	}

	/**
	 * 编辑 练习册/试卷
	 * */
	public function upPractice()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$name = $input['name'];
			$sid = $input['sid'];
			$one_chapter_id = $input['one_chapter_id'];
			$two_chapter_id = $input['two_chapter_id'];
			$year = $input['year'];
			$single_count = $input['single_count'];
			$single_score = $input['single_score'];
			$check_count = $input['check_count'];
			$check_score = $input['check_score'];
			$score = $input['score'];
			$tags = str_replace('，', ',', $input['tags']);
			$is_hot = $input['is_hot'];
			$type = $input['type'];
			$style = $input['style'];
			$sort = $input['sort'];
			$desc = $input['desc'];

			if (empty($name)) $this->error('请输入名称');
			if (empty($sid)) $this->error('请选择所属学科');
			if (empty($one_chapter_id) || empty($two_chapter_id)) $this->error('请选择完整章节-知识点');
			if (empty($single_count) && empty($check_count)) $this->error('单选，多选题数量请至少保持一项数量大于0');
			if (empty($single_score)) $this->error('请输入单选题单题分数');
			if (empty($check_score)) $this->error('请输入多选题单题分数');
			if (empty($score)) $this->error('分数错误');

			$data = array(
				'name' => $name,
				'sid' => $sid,
				'one_chapter_id' => $one_chapter_id,
				'two_chapter_id' => $two_chapter_id,
				'year' => $year,
				'single_count' => $single_count,
				'single_score' => $single_score,
				'check_count' => $check_count,
				'check_score' => $check_score,
				'score' => $score,
				'tags' => $tags,
				'is_hot' => $is_hot,
				'type' => $type,
				'style' => $style,
				'sort' => $sort,
				'desc' => $desc,
				'update_time' => time()
			);

			$res = M('practice')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑成功', U('practiceList'));
			} else {
				$this->error('编辑失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('practice')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$subject_list = M('subject')->field('id,name')->order('sort asc')->select();
			if ($subject_list) {
				foreach ($subject_list as &$val) {
					//章节-知识点 列表
					$chapter_list = M('chapter')->field('id,name')->where(array('sid' => $val['id'], 'pid' => 0))->order('sort asc')->select();
					if ($chapter_list) {
						foreach ($chapter_list as &$v) {
							$son_list = M('chapter')->field('id,name')->where(array('pid' => $v['id']))->order('sort asc')->select();
							$v['son_list'] = $son_list;
						}
					} else {
						$chapter_list = array();
					}
					$val['chapter_list'] = $chapter_list;
				}
			}
			$this->assign('subject_list', $subject_list);
			$this->assign('subject_list_json', json_encode($subject_list));

			//年份
			$year = date('Y');
			$year_list = array();
			for ($i = $year; $i > $year - 15; $i--) {
				$year_list[] = $i;
			}
			$this->assign('year_list', $year_list);

			$this->meta_title = '新增 练习册/试卷';
			$this->display();
		}
	}

	/**
	 * 查看习题
	 * */
	public function practiceExercisesList()
	{
		$id = $_GET['id'];
		$status = $_GET['status'];

		if ($status == 1) {
			$one_chapter_id = cookie('one_chapter_id');
			$two_chapter_id = cookie('two_chapter_id');
			$exercises_ids = cookie('exercises_ids');

			if (empty($one_chapter_id) || empty($two_chapter_id)) $this->error('系统繁忙，请稍后再试');

			$where['one_chapter_id'] = $one_chapter_id;
			$where['two_chapter_id'] = $two_chapter_id;
			if ($exercises_ids) $where['id'] = array('NOT IN', $exercises_ids);

			$count = M('exercises')->where($where)->count();
			$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$list = M('exercises')->field('id,question,option,style,analysis')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
			if ($list) {
				foreach ($list as &$v) {
					$v['option'] = json_decode($v['option'], true);
				}
			}

			$show = $Page->show();// 分页显示输出

			$this->meta_title = '待新增习题';
			$this->assign("_page", $show);
			$this->assign("_list", $list);
		} else {
			$where['pe.practice_id'] = $id;
			$count = M('practice_exercises pe')->join('teach_exercises e ON pe.exercises_id = e.id')->where($where)->count();
			$Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$list = M('practice_exercises pe')->field('pe.id,e.question,e.option,e.style,e.analysis')->join('teach_exercises e ON pe.exercises_id = e.id')->where($where)->order('pe.sort asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
			if ($list) {
				foreach ($list as &$v) {
					$v['option'] = json_decode($v['option'], true);
				}
			}

			$show = $Page->show();// 分页显示输出

			$info = M('practice')->where(array('id' => $id))->find();
			cookie('one_chapter_id', $info['one_chapter_id']);
			cookie('two_chapter_id', $info['two_chapter_id']);

			$have_exercises_list = M('practice_exercises')->field('exercises_id')->where(array('practice_id' => $id))->select();
			$exercises_ids = array_column($have_exercises_list, 'exercises_id');
			cookie('exercises_ids', implode(',', $exercises_ids));

			$this->meta_title = '查看习题';
			$this->assign("_page", $show);
			$this->assign("_list", $list);
		}

		$this->display();
	}

	/**
	 * 确认新增习题
	 * */
	public function addExercisesToPractice()
	{
		$input = I('post.');

		$practice_id = $input['practice_id'];
		$ids = $input['ids'];

		if (empty($ids)) api_return(2, '请勾选需要新增的题目');

		$ids_arr = explode(',', $ids);
		if ($ids_arr) {
			$data = array();
			$add_single_count = 0;
			$add_check_count = 0;
			foreach ($ids_arr as &$v) {
				$style = M('exercises')->where(array('id' => $v))->getField('style');
				if ($style == 1) {
					++$add_single_count;
				} else {
					++$add_check_count;
				}

				$data[] = array(
					'practice_id' => $practice_id,
					'exercises_id' => $v,
					'sort' => 0,
					'update_time' => 0,
					'create_time' => time(),
				);
			}

			$res = M('practice_exercises')->addAll($data);
			if ($res) {
				$exercises_ids = cookie('exercises_ids');
				$exercises_ids = $exercises_ids . ',' . $ids;
				cookie('exercises_ids', $exercises_ids);

				$info = M('practice')->where(array('id' => $practice_id))->find();
				$up_data = array(
					'single_count' => $info['single_count'] + $add_single_count,
					'check_count' => $info['check_count'] + $add_check_count,
					'score' => ($info['single_count'] + $add_single_count) * $info['single_score'] + ($info['check_count'] + $add_check_count) * $info['check_score'],
					'update_time' => time()
				);
				M('practice')->where(array('id' => $practice_id))->save($up_data);

				api_return(1, '新增成功');
			} else {
				api_return(2, '新增失败');
			}
		} else {
			api_return(2, '新增失败');
		}
	}

	/**
	 * 删除 练习册/试卷 题目
	 * */
	public function delPracticeExercises()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('ID缺失');

		$info = M('practice_exercises')->where(array('id' => $id))->find();
		if (!empty($info)) {
			$res = M('practice_exercises')->where(array('id' => $id))->delete();
			if ($res) {
				$style = M('exercises')->where(array('id' => $info['exercises_id']))->getField('style');

				$practice_info = M('practice')->where(array('id' => $info['practice_id']))->find();
				if ($style == 1) {
					$practice_info['single_count'] = $practice_info['single_count'] - 1;
				} else {
					$practice_info['check_count'] = $practice_info['check_count'] - 1;
				}

				$up_data = array(
					'single_count' => $practice_info['single_count'],
					'check_count' => $practice_info['check_count'],
					'score' => $practice_info['single_count'] * $practice_info['single_score'] + $practice_info['check_count'] * $practice_info['check_score'],
					'update_time' => time()
				);
				M('practice')->where(array('id' => $info['practice_id']))->save($up_data);

				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		} else {
			$this->error('数据不存在或已删除');
		}
	}

	/**
	 * 删除 练习册/试卷
	 * */
	public function delPractice()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('ID缺失');

		$count = M('practice')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('practice')->where(array('id' => $id))->delete();
			if ($res) {
				M('practice_exercises')->where(array('practice_id' => $id))->delete();

				$this->success('删除成功');
			} else {
				$this->error('删除失败');
			}
		} else {
			$this->error('数据不存在或已删除');
		}
	}

	/**
	 * 图库分类列表
	 * */
	public function pictureGalleryTypeList()
	{
		$list = M('picture_gallery_type')->select();

		$this->meta_title = '图库分类列表';
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增图库分类
	 * */
	public function addPictureGalleryType()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];

			if (empty($name)) $this->error('分类名缺失');

			$data = array(
				'name' => $name,
			);

			$res = M('picture_gallery_type')->add($data);
			if ($res) {
				$this->success('新增图库分类成功', U('pictureGalleryTypeList'));
			} else {
				$this->error('新增图库分类失败');
			}
		} else {
			$this->meta_title = '新增图库分类';
			$this->display();
		}
	}

	/**
	 * 编辑图库分类
	 * */
	public function upPictureGalleryType()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$name = $input['name'];

			if (empty($id)) $this->error('ID缺失');

			if (empty($name)) $this->error('分类名缺失');

			$data = array(
				'name' => $name,
			);

			$res = M('picture_gallery_type')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑图库分类成功', U('Manage/pictureGalleryTypeList'));
			} else {
				$this->error('编辑图库分类失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('picture_gallery_type')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑图库分类';
			$this->display();
		}
	}

	/**
	 * 删除图库分类
	 * */
	public function delPictureGalleryType()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('图库分类ID缺失');

		$count = M('picture_gallery_type')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('picture_gallery_type')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('图库分类删除成功');
			} else {
				$this->error('图库分类删除失败');
			}
		} else {
			$this->error('图库分类不存在或已删除');
		}
	}

	/**
	 * 图片列表
	 * */
	public function pictureGalleryList()
	{
		$count = M('picture_gallery')->count();
		$Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('picture_gallery')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();

		$this->assign('_list', $list);
		$show = $Page->show();// 分页显示输出
		$this->assign("_page", $show);

		$this->meta_title = '图片列表';
		$this->display();
	}

	/**
	 * 新增图片
	 * */
	public function addPictureGallery()
	{
		if (IS_POST) {
			$input = I('post.');

			$img = $input['img'];
			$type_id = $input['type_id'];
			$style = $input['style'];
			$show_start_time = $input['show_start_time'];
			$show_end_time = $input['show_end_time'];

			if (empty($img)) $this->error('请上传图片');
			if (empty($type_id)) $this->error('请选择分类');
			if ($style == 2) {
				if (empty($show_start_time)) $this->error('请选择开始时间');
				if (empty($show_end_time)) $this->error('请选择结束时间');
			}

			$data = array(
				'img' => $img,
				'type_id' => $type_id,
				'style' => $style,
				'show_start_time' => strtotime($show_start_time),
				'show_end_time' => strtotime($show_end_time),
				'create_time' => time()
			);

			$res = M('picture_gallery')->add($data);
			if ($res) {
				$this->success('新增图片成功', U('pictureGalleryList'));
			} else {
				$this->error('新增图片失败');
			}
		} else {
			$type_list = M('picture_gallery_type')->order('id desc')->select();
			$this->assign('type_list', $type_list);

			$this->meta_title = '新增图片';
			$this->display();
		}
	}

	/**
	 * 编辑图片
	 * */
	public function upPictureGallery()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$img = $input['img'];
			$type_id = $input['type_id'];
			$style = $input['style'];
			$show_start_time = $input['show_start_time'];
			$show_end_time = $input['show_end_time'];

			if (empty($id)) api_return(2, '图片ID缺失');
			if (empty($img)) $this->error('请上传图片');
			if (empty($type_id)) $this->error('请选择分类');
			if ($style == 2) {
				if (empty($show_start_time)) $this->error('请选择开始时间');
				if (empty($show_end_time)) $this->error('请选择结束时间');
			}

			$data = array(
				'img' => $img,
				'type_id' => $type_id,
				'style' => $style,
				'show_start_time' => strtotime($show_start_time),
				'show_end_time' => strtotime($show_end_time),
				'update_time' => time()
			);

			$res = M('picture_gallery')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑图片成功', U('pictureGalleryList'));
			} else {
				$this->error('编辑图片失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('picture_gallery')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$type_list = M('picture_gallery_type')->order('id desc')->select();
			$this->assign('type_list', $type_list);

			$this->meta_title = '编辑图片';
			$this->display();
		}
	}

	/**
	 * 删除图片
	 * */
	public function delPictureGallery()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('图片ID缺失');

		$count = M('picture_gallery')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('picture_gallery')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('图片删除成功');
			} else {
				$this->error('图片删除失败');
			}
		} else {
			$this->error('图片不存在或已删除');
		}
	}

	/**
	 * 标签列表
	 * */
	public function feedbackTagList()
	{
		$count = M('feedback_tag')->count();
		$Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('feedback_tag')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '标签列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增标签
	 * */
	public function addFeedbackTag()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];

			if (empty($name)) $this->error('标签缺失');

			$count = M('feedback_tag')->where(array('name' => $name))->count();
			if ($count > 0) $this->error('该标签已存在');

			$data = array(
				'name' => $name,
				'create_time' => time()
			);

			$res = M('feedback_tag')->add($data);
			if ($res) {
				$this->success('新增标签成功', U('feedbackTagList'));
			} else {
				$this->error('新增标签失败');
			}
		} else {
			$this->meta_title = '新增标签';
			$this->display();
		}
	}

	/**
	 * 编辑标签
	 * */
	public function upFeedbackTag()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$name = $input['name'];

			if (empty($id)) $this->error('ID缺失');
			if (empty($name)) $this->error('标签缺失');

			$count = M('feedback_tag')->where(array('name' => $name, array('id' => array('neq', $id))))->count();
			if ($count > 0) $this->error('该标签已存在');

			$data = array(
				'name' => $name,
				'update_time' => time()
			);

			$res = M('feedback_tag')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑标签成功', U('Manage/feedbackTagList'));
			} else {
				$this->error('编辑标签失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('feedback_tag')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑标签';
			$this->display();
		}
	}

	/**
	 * 删除标签
	 * */
	public function delFeedbackTag()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('标签ID缺失');

		$count = M('feedback_tag')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('feedback_tag')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('标签删除成功');
			} else {
				$this->error('标签删除失败');
			}
		} else {
			$this->error('标签不存在或已删除');
		}
	}

	/**
	 * 意见反馈列表
	 * */
	public function feedbackList()
	{
		$count = M('feedback')->count();
		$Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('feedback')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '意见反馈列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 查看详情
	 * */
	public function upFeedback()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$is_adopt = $input['is_adopt'];

			if (empty($id)) $this->error('反馈意见ID缺失');

			$data = array(
				'is_adopt' => $is_adopt,
				'update_time' => time()
			);
			$res = M('feedback')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑意见反馈成功', U('Manage/feedbackList'));
			} else {
				$this->error('编辑意见反馈失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('feedback')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			M('feedback')->where(array('id' => $id))->setField('is_read', 1);

			$this->meta_title = '查看详情';
			$this->display();
		}
	}

	/**
	 * 删除意见反馈
	 * */
	public function delFeedback()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('意见反馈ID缺失');

		$count = M('feedback')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('feedback')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('意见反馈删除成功');
			} else {
				$this->error('意见反馈删除失败');
			}
		} else {
			$this->error('意见反馈不存在或已删除');
		}
	}

	/**
	 * 资讯分类列表
	 * */
	public function informationTypeList()
	{
		$list = M('information_type')->order('id desc')->select();

		$this->meta_title = '资讯分类列表';
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增资讯分类
	 * */
	public function addInformationType()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];

			if (empty($name)) $this->error('分类名缺失');

			$count = M('information_type')->where(array('name' => $name))->count();
			if ($count > 0) $this->error('该分类名已存在，请重新输入');

			$data = array(
				'name' => $name,
				'create_time' => time()
			);

			$res = M('information_type')->add($data);
			if ($res) {
				$this->success('新增资讯分类成功', U('informationTypeList'));
			} else {
				$this->error('新增资讯分类失败');
			}
		} else {
			$this->meta_title = '新增资讯分类';
			$this->display();
		}
	}

	/**
	 * 编辑资讯分类
	 * */
	public function upInformationType()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$name = $input['name'];

			if (empty($id)) $this->error('ID缺失');
			if (empty($name)) $this->error('分类名缺失');

			$count = M('information_type')->where(array('name' => $name, 'id' => array('neq', $id)))->count();
			if ($count > 0) $this->error('该分类名已存在，请重新输入');

			$data = array(
				'name' => $name,
				'update_time' => time()
			);

			$res = M('information_type')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑资讯分类成功', U('Manage/informationTypeList'));
			} else {
				$this->error('编辑资讯分类失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('information_type')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑资讯分类';
			$this->display();
		}
	}

	/**
	 * 删除资讯分类
	 * */
	public function delInformationType()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('资讯分类ID缺失');

		$count = M('information_type')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('information_type')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('资讯分类删除成功');
			} else {
				$this->error('资讯分类删除失败');
			}
		} else {
			$this->error('资讯分类不存在或已删除');
		}
	}

	/**
	 * 资讯列表
	 * */
	public function informationList()
	{
		$count = M('information')->count();
		$Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('information')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '资讯列表';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增资讯
	 * */
	public function addInformation()
	{
		if (IS_POST) {
			$input = I('post.');

			$title = $input['title'];
			$img = $input['img'];
			$type_id = $input['type_id'];
			$from = $input['from'];
			$content = $input['content'];

			if (empty($title)) $this->error('标题缺失');
			if (empty($img)) $this->error('请上传封面图');
			if (empty($type_id)) $this->error('分类缺失');
			if (empty($from)) $this->error('来源缺失');
			if (empty($content)) $this->error('内容缺失');

			$data = array(
				'title' => $title,
				'img' => $img,
				'type_id' => $type_id,
				'from' => $from,
				'content' => $content,
				'create_time' => time()
			);

			$res = M('information')->add($data);
			if ($res) {
				$this->success('新增资讯成功', U('Manage/informationList'));
			} else {
				$this->error('新增资讯失败');
			}
		} else {
			$type_list = M('information_type')->select();
			$this->assign('type_list', $type_list);

			$this->meta_title = '新增资讯';
			$this->display();
		}
	}

	/**
	 * 编辑资讯
	 * */
	public function upInformation()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$title = $input['title'];
			$img = $input['img'];
			$type_id = $input['type_id'];
			$from = $input['from'];
			$content = $input['content'];

			if (empty($id)) $this->error('ID缺失');

			if (empty($title)) $this->error('标题缺失');
			if (empty($img)) $this->error('请上传封面图');
			if (empty($type_id)) $this->error('分类缺失');
			if (empty($from)) $this->error('来源缺失');
			if (empty($content)) $this->error('内容缺失');

			$data = array(
				'title' => $title,
				'img' => $img,
				'type_id' => $type_id,
				'from' => $from,
				'content' => $content,
				'update_time' => time()
			);

			$res = M('information')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑资讯成功', U('Manage/informationList'));
			} else {
				$this->error('编辑资讯失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('information')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$type_list = M('information_type')->select();
			$this->assign('type_list', $type_list);

			$this->meta_title = '编辑资讯';
			$this->display();
		}
	}

	/**
	 * 删除资讯
	 * */
	public function delInformation()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('资讯ID缺失');

		$count = M('information')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('information')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('资讯删除成功');
			} else {
				$this->error('资讯删除失败');
			}
		} else {
			$this->error('资讯不存在或已删除');
		}
	}

	/**
	 * 显示分类树，仅支持内部调
	 * @param        array        $tree        分类树
	 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
	 */
	public function tree($tree = null)
	{
		C('_SYS_GET_CATEGORY_TREE_');
		$this->assign('tree', $tree);
		$this->display('tree');
	}

	/**
	 * 显示分类树，仅支持内部调（课程分类）
	 * @param        array        $tree        分类树
	 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
	 */
	public function videoTypeTree($tree = null)
	{
		C('_SYS_GET_CATEGORY_TREE_');
		$this->assign('tree', $tree);
		$this->display('videoTypeTree');
	}

	/**
	 * 显示分类树，仅支持内部调（课程分类）
	 * @param        array        $tree        分类树
	 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
	 */
	public function faceTypeTree($tree = null)
	{
		C('_SYS_GET_CATEGORY_TREE_');
		$this->assign('tree', $tree);
		$this->display('faceTypeTree');
	}

	/**
	 * 书籍分类列表
	 * */
	public function goodsTypeList()
	{
		$tree = D('goods_type')->getTree(0, 'id,name,sort,pid');
		$this->assign('tree', $tree);

		$this->meta_title = '书籍分类列表';
		$this->display();
	}

	/**
	 * 新增书籍分类
	 * */
	public function addGoodsType()
	{
		if (IS_POST) {
			$input = I('post.');

			$name = $input['name'];
			$sort = $input['sort'] ? $input['sort'] : 0;
			$pid = $input['pid'] ? $input['pid'] : 0;

			if (empty($name)) $this->error('请输入分类名');

			$count = M('goods_type')->where(array('name' => $name, 'pid' => $pid))->count();
			if ($count > 0) $this->error('分类名重复，请重新输入');

			$data = array(
				'name' => $name,
				'sort' => $sort,
				'pid' => $pid,
				'create_time' => time()
			);
			$res = M('goods_type')->add($data);
			if ($res) {
				$this->success('新增分类成功', U('Manage/goodsTypeList'));
			} else {
				$this->error('新增分类失败');
			}
		} else {
			$this->meta_title = '新增书籍分类';
			$this->display();
		}
	}

	/**
	 * 编辑书籍分类
	 * */
	public function upGoodsType()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$name = $input['name'];
			$sort = $input['sort'] ? $input['sort'] : 0;
			$pid = $input['pid'] ? $input['pid'] : 0;

			if (empty($id)) $this->error('分类ID缺失');
			if (empty($name)) $this->error('请输入分类名');

			$count = M('goods_type')->where(array('name' => $name, 'pid' => $pid, 'id' => array('neq', $id)))->count();
			if ($count > 0) $this->error('分类名重复，请重新输入');

			$data = array(
				'name' => $name,
				'sort' => $sort,
				'pid' => $pid,
				'create_time' => time()
			);
			$res = M('goods_type')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑分类成功', U('Manage/goodsTypeList'));
			} else {
				$this->error('编辑分类失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('goods_type')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑书籍分类';
			$this->display();
		}
	}

	/**
	 * 删除书籍分类
	 * */
	public function delGoodsType()
	{
		$id = $_GET['id'];
		$pid = $_GET['pid'];

		if (empty($id)) $this->error('分类ID缺失');

		$count = M('goods_type')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('goods_type')->where(array('id' => $id))->delete();
			if ($res) {
				if ($pid == 0) {
					M('goods_type')->where(array('pid' => $id))->delete();
				}

				$this->success('分类删除成功');
			} else {
				$this->error('分类删除失败');
			}
		} else {
			$this->error('分类不存在或已删除');
		}
	}

	public function live()
	{
		$roomID = '123456';
		$this->assign('roomID', $roomID);
		$SDKAppID = C('YUNSDKAPPID');
		$this->assign('SDKAppID', $SDKAppID);
		$userID = 'Dave';
		$this->assign('userID', $userID);
		$liveDomainName = 'administrator';
		$this->assign('liveDomainName', $liveDomainName);
		$streamid = '27240_654321';
		$this->assign('streamid', $streamid);
		$userSig = getUserSig($userID);
		echo $userSig;
		$this->assign('userSig', $userSig);
		$this->display();
	}

	public function pull()
	{
		$roomID = '123456';
		$this->assign('roomID', $roomID);
		$SDKAppID = C('YUNSDKAPPID');
		$this->assign('SDKAppID', $SDKAppID);
		$userID = 'Dave_pull';
		$this->assign('userID', $userID);
		$userSig = getUserSig($userID);
		echo $userSig;
		$this->assign('userSig', $userSig);
		$this->display();
	}

	public function im()
	{
		$userID = 'web_user';
		$this->assign('userID', $userID);
		$userSig = getUserSig($userID);
		$this->assign('userSig', $userSig);
		$this->display();
	}

	public function test()
	{
		//公有空间视频播放链接
		//$url = 'https://excel.alijuly.cn/video1608545750.mp4';

		vendor('qiniuyun/autoload');
		//私有空间视频播放链接
		$accessKey = C('AK');  //秘钥
		$secretKey = C('SK');  //秘钥
		$auth = new Auth($accessKey, $secretKey);  //实例化
		$url = $auth->privateDownloadUrl(C('CDN_URL') . '1615876231594.mp4', 3600 * 24 * 30);
		$this->assign('url', $url);

		//分配token到页面
		$bucket = C('BUCKET');//存储空间
		$token = $auth->uploadToken($bucket);
		$this->assign('token', $token);

		$this->display();
	}

	/**
	 * 验证码发送记录
	 * */
	public function smsList()
	{
		$count = M('sms_code')->count();
		$this->assign('count', $count);

		$Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$list = M('sms_code')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

		$show = $Page->show();// 分页显示输出

		$this->meta_title = '验证码发送记录';
		$this->assign("_page", $show);
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 售后类型列表
	 * */
	public function afterTypeList()
	{
		$where['type'] = 1;

		$list = M('after_data')->where($where)->order('id asc')->select();

		$this->meta_title = '售后类型列表';
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增售后类型
	 * */
	public function addAfterType()
	{
		if (IS_POST) {
			$input = I('post.');

			$desc = $input['desc'];
			$is_back_price = $input['is_back_price'];

			if (empty($desc)) $this->error('描述缺失');
			$count = M('after_data')->where(array('desc' => $desc, 'type' => 1))->count();
			if ($count) $this->error('该售后类型已经存在');

			$data = array(
				'desc' => $desc,
				'is_back_price' => $is_back_price,
				'create_time' => time()
			);

			$res = M('after_data')->add($data);
			if ($res) {
				$this->success('新增售后类型成功', U('afterTypeList'));
			} else {
				$this->error('新增售后类型失败');
			}
		} else {
			$this->meta_title = '新增售后类型';
			$this->display();
		}
	}

	/**
	 * 编辑售后类型
	 * */
	public function upAfterType()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$desc = $input['desc'];
			$is_back_price = $input['is_back_price'];

			if (empty($id)) $this->error('ID缺失');
			$count = M('after_data')->where(array('desc' => $desc, 'type' => 1, 'id' => array('neq', $id)))->count();
			if ($count) $this->error('该售后类型已经存在');

			if (empty($desc)) $this->error('描述缺失');

			$data = array(
				'desc' => $desc,
				'is_back_price' => $is_back_price,
				'update_time' => time()
			);

			$res = M('after_data')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑售后类型成功', U('Manage/afterTypeList'));
			} else {
				$this->error('编辑售后类型失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('after_data')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑售后类型';
			$this->display();
		}
	}

	/**
	 * 删除售后类型
	 * */
	public function delAfterType()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('售后类型ID缺失');

		$count = M('after_data')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('after_data')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('售后类型删除成功');
			} else {
				$this->error('售后类型删除失败');
			}
		} else {
			$this->error('售后类型不存在或已删除');
		}
	}

	/**
	 * 售后原因列表
	 * */
	public function afterReasonList()
	{
		$where['type'] = 2;

		$list = M('after_data')->where($where)->order('id asc')->select();

		$this->meta_title = '售后原因列表';
		$this->assign("_list", $list);
		$this->display();
	}

	/**
	 * 新增售后原因
	 * */
	public function addAfterReason()
	{
		if (IS_POST) {
			$input = I('post.');

			$desc = $input['desc'];

			if (empty($desc)) $this->error('描述缺失');
			$count = M('after_data')->where(array('desc' => $desc, 'type' => 2))->count();
			if ($count) $this->error('该售后原因已经存在');

			$data = array(
				'desc' => $desc,
				'type' => 2,
				'create_time' => time()
			);

			$res = M('after_data')->add($data);
			if ($res) {
				$this->success('新增售后原因成功', U('afterReasonList'));
			} else {
				$this->error('新增售后原因失败');
			}
		} else {
			$this->meta_title = '新增售后原因';
			$this->display();
		}
	}

	/**
	 * 编辑售后原因
	 * */
	public function upAfterReason()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];

			$desc = $input['desc'];

			if (empty($id)) $this->error('ID缺失');
			$count = M('after_data')->where(array('desc' => $desc, 'type' => 2, 'id' => array('neq', $id)))->count();
			if ($count) $this->error('该售后原因已经存在');

			if (empty($desc)) $this->error('描述缺失');

			$data = array(
				'desc' => $desc,
				'update_time' => time()
			);

			$res = M('after_data')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑售后类型成功', U('Manage/afterReasonList'));
			} else {
				$this->error('编辑售后类型失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('after_data')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$this->meta_title = '编辑售后原因';
			$this->display();
		}
	}

	/**
	 * 删除售后原因
	 * */
	public function delAfterReason()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('售后原因ID缺失');

		$count = M('after_data')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('after_data')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('售后原因删除成功');
			} else {
				$this->error('售后原因删除失败');
			}
		} else {
			$this->error('售后原因不存在或已删除');
		}
	}

	/**
	 * 消息列表
	 * */
	public function msgList()
	{
		$where['type'] = 3;

		$list = M('msg')->where($where)->order('id asc')->select();

		$this->meta_title = '消息列表';
		$this->assign("_list", $list);
		$this->display();
	}

	public function searchUser()
	{
		$input = I('post.');

		$mobile = $input['mobile'];

		$where = " FIND_IN_SET(2, user_type) ";
		if ($mobile) $where .= " && mobile like '%" . $mobile . "%' ";

		$list = M('ucenter_member')->field('id,username')->where($where)->select();
		if ($list) {
			api_return(1, '获取数据成功', $list);
		} else {
			api_return(2, '暂无更多数据');
		}
	}

	/**
	 * 新增消息
	 * */
	public function addMsg()
	{
		if (IS_POST) {
			$input = I('post.');

			$title = $input['title'];
			$uid = $input['uid'];
			$sys_content = $input['sys_content'];

			if (empty($title)) $this->error('请输入标题');
			if (empty($sys_content)) $this->error('请输入内容');

			$data = array(
				'uid' => $uid,
				'title' => $title,
				'sys_content' => $sys_content,
				'type' => 3,
				'create_time' => time()
			);

			$res = M('msg')->add($data);
			if ($res) {
				$this->success('新增消息成功', U('msgList'));
			} else {
				$this->error('新增消息失败');
			}
		} else {
			$where = " FIND_IN_SET(2, user_type) ";
			$user_list = M('ucenter_member')->field('id,username')->where($where)->select();
			$this->assign('user_list', $user_list);

			$this->meta_title = '新增消息';
			$this->display();
		}
	}

	/**
	 * 编辑消息
	 * */
	public function upMsg()
	{
		if (IS_POST) {
			$input = I('post.');

			$id = $input['id'];
			$title = $input['title'];
			$uid = $input['uid'];
			$sys_content = $input['sys_content'];

			if (empty($id)) $this->error('消息ID缺失');
			if (empty($title)) $this->error('请输入标题');
			if (empty($sys_content)) $this->error('请输入内容');

			$data = array(
				'uid' => $uid,
				'title' => $title,
				'sys_content' => $sys_content,
				'type' => 3,
				'update_time' => time()
			);

			$res = M('msg')->where(array('id' => $id))->save($data);
			if ($res) {
				$this->success('编辑消息成功', U('msgList'));
			} else {
				$this->error('编辑消息失败');
			}
		} else {
			$id = $_GET['id'];

			$info = M('msg')->where(array('id' => $id))->find();
			$this->assign('info', $info);

			$where = " FIND_IN_SET(2, user_type) ";
			$user_list = M('ucenter_member')->field('id,username')->where($where)->select();
			$this->assign('user_list', $user_list);

			$this->meta_title = '编辑消息';
			$this->display();
		}
	}

	/**
	 * 删除消息
	 * */
	public function delMsg()
	{
		$id = $_GET['id'];

		if (empty($id)) $this->error('消息ID缺失');

		$count = M('msg')->where(array('id' => $id))->count();
		if ($count) {
			$res = M('msg')->where(array('id' => $id))->delete();

			if ($res) {
				$this->success('消息删除成功');
			} else {
				$this->error('消息删除失败');
			}
		} else {
			$this->error('消息不存在或已删除');
		}
	}

}
